package org.jeecg.glr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.GlrRadio;
import org.jeecg.glr.entity.GlrRadioTask;
import org.jeecg.glr.service.IGlrRadioServerService;
import org.jeecg.glr.service.IGlrRadioTaskService;
import org.jeecg.glr.util.GLRConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 音柱定时任务
 * @Author: jeecg-boot
 * @Date:   2020-02-13
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrRadioTask")
@Slf4j
public class GlrRadioTaskController extends JeecgController<GlrRadioTask, IGlrRadioTaskService> {
	@Autowired
	private IGlrRadioTaskService glrRadioTaskService;
	 @Autowired
	 private IGlrRadioServerService glrRadioServerService;
	
	/**
	 * 分页列表查询
	 *
	 * @param glrRadioTask
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GlrRadioTask glrRadioTask,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) throws Exception {
		QueryWrapper<GlrRadioTask> queryWrapper = QueryGenerator.initQueryWrapperCode(glrRadioTask, req.getParameterMap());
		Page<GlrRadioTask> page = new Page<GlrRadioTask>(pageNo, pageSize);
		IPage<GlrRadioTask> pageList = glrRadioTaskService.page(page, queryWrapper);
		List<GlrRadioTask> list=pageList.getRecords();
		for (GlrRadioTask domain: list) {
			if (GLRConstant.GLR_RADIO_TASK_STATUS_0.equals(domain.getStatus())){
				domain.setStatuSwitch0(true);
			}else{
				domain.setStatuSwitch0(false);
			}
			if (GLRConstant.GLR_RADIO_TASK_STATUS_3.equals(domain.getStartStatus())){
				domain.setStatuSwitch1(true);
			}else{
				domain.setStatuSwitch1(false);
			}
		}
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param glrRadioTask
	 * @return
	 */
	@AutoLog(value = "添加音柱任务",logType= CommonConstant.LOG_TYPE_2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GlrRadioTask glrRadioTask) {
		glrRadioTask.setStatus(GLRConstant.GLR_RADIO_TASK_STATUS_1);
		glrRadioTask.setStartStatus(GLRConstant.GLR_RADIO_TASK_STATUS_4);
		glrRadioTaskService.save(glrRadioTask);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param glrRadioTask
	 * @return
	 */
	@AutoLog(value = "修改音柱任务",logType= CommonConstant.LOG_TYPE_2)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GlrRadioTask glrRadioTask) {
		boolean flag= false;
		try {
			flag = glrRadioTaskService.updateTaskById(glrRadioTask);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(flag){
			return Result.ok("编辑成功!");
		}
		return Result.error("编辑失败!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "删除音柱任务",logType= CommonConstant.LOG_TYPE_2)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id){
        boolean flag= false;
        try {
            flag = glrRadioTaskService.deleteTasks(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){
			return Result.ok("删除成功!");
		}
		return Result.error("删除失败!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "批量删除音柱任务",logType= CommonConstant.LOG_TYPE_2)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        boolean flag= false;
        try {
            flag = glrRadioTaskService.deleteTasks(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){
            return Result.ok("批量删除成功!");
        }
        return Result.error("批量删除失败!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GlrRadioTask glrRadioTask = glrRadioTaskService.getById(id);
		if(glrRadioTask==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(glrRadioTask);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param glrRadioTask
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrRadioTask glrRadioTask) {
        return super.exportXls(request, glrRadioTask, GlrRadioTask.class, "音柱定时任务");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrRadioTask.class);
    }

	 /**
	  * 创建音柱任务
	  * @param glrRadioTask
	  * @return
	  */
	 @AutoLog(value = "创建音柱任务",logType= CommonConstant.LOG_TYPE_2)
	 @PostMapping(value = "/createTask")
	 public Result<?> doCreateTask(@RequestBody GlrRadioTask glrRadioTask) {
    	try{
			glrRadioTaskService.doCreateTask(glrRadioTask);
		}catch (Exception ex){
			return Result.error(ex.getMessage());
		}
		return Result.ok("操作成功！");
	 }

	 /**
	  * 执行音柱任务
	  * @param glrRadioTask
	  * @return
	  */
	 @AutoLog(value = "执行音柱任务",logType= CommonConstant.LOG_TYPE_2)
	 @PostMapping(value = "/executeTask")
	 public Result<?> doExecuteTask(@RequestBody GlrRadioTask glrRadioTask) {
		 try{
			 glrRadioTaskService.executeTask(glrRadioTask);
		 }catch (Exception ex){
			 return Result.error(ex.getMessage());
		 }
		 return Result.ok("操作成功！");
	 }

	 /**
	  * 设置音柱任务音量
	  * @param glrRadioTask
	  * @return
	  */
	 @AutoLog(value = "设置音柱任务音量",logType= CommonConstant.LOG_TYPE_2)
	 @PostMapping(value = "/setRadioVol")
	 public Result<?> setRadioVol(@RequestBody GlrRadioTask glrRadioTask) {
		 glrRadioServerService.setRadioVol(glrRadioTask);
		 return Result.ok("设置音量成功!");
	 }

}
